package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.entity.WebLog;
import com.woniu.service.LogService;
import com.woniu.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("api/logs")
@CrossOrigin
public class LogController {
    @Autowired
    private LogService logService;

    @GetMapping("/selectAll")
    public ResultObj getLogs(String action, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sDate = null;
        Date eDate = null;
        try {
            if(startTime!=null && startTime.trim()!="" && endTime!=null && endTime.trim()!="") {
                sDate = sdf.parse(startTime);
                eDate = sdf.parse(endTime);
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        PageInfo<WebLog> webLogPageInfo = logService.selectAll(action, sDate, eDate, pageNum, pageSize);
        return ResultObj.ok(webLogPageInfo);
    }
}
